//
//  ViewController.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (nonatomic,strong) NSMutableArray *list;

@end

@implementation ViewController

- (NSMutableArray *)list
{
    if(_list == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if([fm fileExistsAtPath:[self plistPath]])
        {
            _list = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
          
        }
        else
        {
            _list = [NSMutableArray array];
           
        }
    }
    return _list;
}
- (NSString *)plistPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
    return filePath;
}
- (void)savePlist
{
    // 写入到文件
    [self.list writeToFile:[self plistPath] atomically:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateinfo1) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateinfo2) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateinfo3) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateinfo4) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    }

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    float redcolor=[d floatForKey:@"red"]/255.0;
    float bluecolor=[d floatForKey:@"blue"]/255.0;
    float greeencolor=[d floatForKey:@"green"]/255.0;
    self.view.backgroundColor=[UIColor colorWithRed:redcolor green:greeencolor blue:bluecolor alpha:255/255.0];
       
}
-(void)reload
{
    NSMutableString *mstr=[[NSMutableString alloc]init];
    for(long int i=self.list.count-1;i>=0;i--)
    {
        [mstr insertString:[NSString stringWithFormat:@"%@\n",self.list[i]] atIndex:mstr.length];
        NSLog(@"%lu",self.list.count);
    }
    self.textview.text=[NSString stringWithFormat:@"%@",mstr];

   
}
-(void)updateinfo1
{
    
    NSDate *now = [NSDate date];
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setDateFormat:@"HH:mm:ss"];
    NSString *str=[f stringFromDate:now];
    NSString *s=[NSString stringWithFormat:@"%@,将取消活动",str];
    [self.list addObject:s];
    [self savePlist];
    [self reload];
}

-(void)updateinfo2
{
    
    NSDate *now = [NSDate date];
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setDateFormat:@"HH:mm:ss"];
    NSString *str=[f stringFromDate:now];

    NSString *s=[NSString stringWithFormat:@"%@,已经进入后台",str];
    
    [self.list addObject:s];
    [self savePlist];
    [self reload];
}
-(void)updateinfo3
{
    
    NSDate *now = [NSDate date];
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setDateFormat:@"HH:mm:ss"];
    NSString *str=[f stringFromDate:now];

    NSString *s=[NSString stringWithFormat:@"%@,将进入前台",str];
    
    [self.list addObject:s];
    [self savePlist];
    [self reload];
}
-(void)updateinfo4
{
    
    NSDate *now = [NSDate date];
    NSDateFormatter *f=[[NSDateFormatter alloc]init];
    [f setDateFormat:@"HH:mm:ss"];
    NSString *str=[f stringFromDate:now];

    NSString *s=[NSString stringWithFormat:@"%@,已经激活",str];
    
    [self.list addObject:s];
    [self savePlist];
    [self reload];
}
@end
